package boun.swe.bob.service;

import boun.swe.bob.entities.Friend;
import boun.swe.bob.entities.FriendRequestPK;
import boun.swe.bob.entities.FriendsPK;
import boun.swe.bob.entities.User;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * This controller is used for my friend requests page actions
 */
public class MyFriendRequestsController extends BaseController {

    /**
     * Shows the page
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        myModel.put("himself", true);
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
        return new ModelAndView("myFriendRequests", "model", myModel);
    }


    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String accept = httpServletRequest.getParameter("accept");
        String reject = httpServletRequest.getParameter("reject");

        int requestingUserId = Integer.parseInt(httpServletRequest.getParameter("requestingUserId"));
        int requestedUserId = Integer.parseInt(httpServletRequest.getParameter("requestedUserId"));

        if(null != accept) {

            FriendsPK friendPK = new FriendsPK();
            friendPK.setUser1Id(requestingUserId);
            friendPK.setUser2Id(requestedUserId);
            Friend friend = friendDao.findByPrimaryKey(friendPK);
            friend.setApproved(1);
            friendDao.update(friend);

            friendPK = new FriendsPK();
            friendPK.setUser1Id(requestedUserId);
            friendPK.setUser2Id(requestingUserId);
            friend = friendDao.findByPrimaryKey(friendPK);
            friend.setApproved(1);
            friendDao.update(friend);


            User usr = userDao.findByPrimaryKey(requestedUserId);
            String namesurname = usr.getName() + " " + usr.getSurname();

            mailSender.send(friend.friend.getUsername(),"Friend Request", namesurname + " has accepted your friend request");

        }

        if(null != reject) {
            FriendsPK friendPK = new FriendsPK();
            friendPK.setUser1Id(requestingUserId);
            friendPK.setUser2Id(requestedUserId);
            friendDao.deleteByPrimaryKey(friendPK);

            friendPK = new FriendsPK();
            friendPK.setUser1Id(requestedUserId);
            friendPK.setUser2Id(requestingUserId);
            friendDao.deleteByPrimaryKey(friendPK);
        }

        FriendRequestPK friendRequestPK = new FriendRequestPK();
        friendRequestPK.setRequestedUserId(requestedUserId);
        friendRequestPK.setRequestingUserId(requestingUserId);
        friendRequestDao.deleteByPrimaryKey(friendRequestPK);

        Map<String, Object> myModel = Maps.newHashMap();
        myModel.put("himself", true);
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
        return new ModelAndView("myFriendRequests", "model", myModel);

    }

}
